package nmsu.ppe.agendabuddy;

import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.content.Intent;
import android.view.View;

public class EditEventActivity extends Activity
{
        private Calendar viewDate;
        private DBAdapter dba = new DBAdapter(this);
        private String[] info;
        DatePicker dateBox;
        TimePicker timeBox;
        EditText   descriptionBox;

        /** Called whenever the activity is created. */
/**		  
* @param savedInstanceState saved current info, uses for activity creation
**/		  
		  
		  
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);

                // set the view that will be seen
                setContentView(R.layout.edit_event);

                // get the info from the event that was clicked
                Bundle extras = getIntent().getExtras();
                info = (String[]) extras.get("infoArray");
                viewDate = (Calendar) extras.get("vd");

                // gather elements to set values for
                dateBox = (DatePicker) findViewById( R.id.datePicker_EditEventDate );
                timeBox = (TimePicker) findViewById( R.id.timePicker_EditEventTime );
                descriptionBox = (EditText) findViewById( R.id.editText_EditEventDescription );

                // set date spinner to the clicked event date
                dateBox.init( viewDate.get( Calendar.YEAR ), 
                                        viewDate.get( Calendar.MONTH ),
                                        viewDate.get( Calendar.DATE ), 
                                        null );

                // set time spinner to the clicked event time
                int isPM = 0;
                if( info[0].substring(info[0].length()-2).equals("PM") )
                        isPM = 12;

                timeBox.setCurrentHour( isPM + Integer.valueOf( 
                                info[0].substring(
                                                0, 
                                                info[0].indexOf(':')) ));

                timeBox.setCurrentMinute( Integer.valueOf( 
                                info[0].substring(
                                                info[0].indexOf(':')+1, 
                                                info[0].indexOf(' ')    ) ));

                // set description to event description
                descriptionBox.setText(info[1]);
        }
/**
* @param v used to help navigate views
**/
        public void finishEditEvent(View v)
        {
                // update the data base
                dba.open();
                
                // get the time
                String time = "";
                if(timeBox.getCurrentHour() < 10)
                        time += "0";
                time += timeBox.getCurrentHour() + ":";
                if(timeBox.getCurrentMinute() < 10)
                        time += "0";
                time += timeBox.getCurrentMinute();
                
                // update the database
                dba.updateEvent(
                                Long.valueOf(info[2]),
                                dateBox.getMonth() + ":" + dateBox.getDayOfMonth() + ":" + dateBox.getYear(), 
                                time, 
                                descriptionBox.getText().toString().replaceAll("\n", " ") 
                                );
                
                dba.close();
                
                // end the activity
                setResult(RESULT_OK, (new Intent()));
                finish();
        }

}